package com.lawyer.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lawyer.entity.Coupon;
import com.lawyer.entity.Lawyer;
import com.lawyer.entity.MealConfiguration;
import com.lawyer.entity.RoomInfo;
import com.lawyer.model.Message;
import com.lawyer.model.MessageConstant;
import com.lawyer.repository.CouponRepository;
import com.lawyer.repository.LawyerRepository;
import com.lawyer.repository.MealConfigurationRepository;
import com.lawyer.repository.RoomInfoRepository;
import com.lawyer.service.PushService;
import com.tencentyun.TLSSigAPIv2;

@RestController
@RequestMapping(value = "/room")
@Api(tags = "房间相关接口")
@SuppressWarnings("all")
public class RoomInfoController {

	@Autowired
	private LawyerRepository lawyerRepository;
	@Autowired
	private CouponRepository couponRepository;

	@Autowired
	private PushService pushService;
	
	@Autowired
	private MealConfigurationRepository mealConfigurationRepository;

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	protected final Integer sdkAppid = 1400599652;
	protected final String secretKey = "698263332cbfe35658eb7eae3a366a4763ac8de59a24de0d457aebf15a1031b8";

	@Autowired
	private RoomInfoRepository roomInfoRepository;

	@ApiOperation(httpMethod = "GET", value = "创建房间", notes = "优惠券的CouponId;套餐的mealId")
	@RequestMapping(value = "/createRoom", method = RequestMethod.GET)
	public HttpEntity<?> createRoom(String userId, String lawyerId,Integer couponId,Integer mealId) {
		Message msg = new Message();
		RoomInfo info = new RoomInfo();
		//设置优惠套餐为已使用
		if (mealId != null) {
			MealConfiguration meal = mealConfigurationRepository.findOne(mealId);
			if (meal != null) {
				info.setMealId(meal.getId());
				info.setMealName(meal.getDescription());
				info.setPrice(meal.getPrice());
				info.setTime(meal.getTime());
			}
		}else{
			Coupon coupon = couponRepository.findByUserIdAndIsUsedAndId(userId,false,couponId);
			if (coupon != null) {
				coupon.setIsUsed(true);
				couponRepository.save(coupon);
			}
		}

		Lawyer lawyer = lawyerRepository.findOne(lawyerId);
		if (lawyer == null) {
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage("找不到律师：" + lawyerId);
			return new ResponseEntity<>(msg, HttpStatus.OK);
		}
		TLSSigAPIv2 api = new TLSSigAPIv2(sdkAppid, secretKey);
		String userSig = api.genUserSig(userId, 180 * 86400);
		try {
			
			info.setUserSig(userSig);
			info.setUserId(userId);
			info.setLawyerId(lawyerId);
			info.setSdkAppId(sdkAppid);
			info = roomInfoRepository.save(info);
			info.setRoomId(info.getId());
			msg.setData(info);

			// 推送消息
			if (lawyer.getCid() != null) {
				pushService.pushToLawyer(lawyer.getCid(), info);
			}
		} catch (Exception e) {
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage("code无效");
		}

		return new ResponseEntity<>(msg, HttpStatus.OK);
	}

	@ApiOperation(httpMethod = "GET", value = "生成签名", notes = "生成签名")
	@RequestMapping(value = "/createuUserSig", method = RequestMethod.GET)
	public HttpEntity<?> createuUserSig(String lawyerId) {
		Message msg = new Message();

		try {
			TLSSigAPIv2 api = new TLSSigAPIv2(sdkAppid, secretKey);
			String userSig = api.genUserSig(lawyerId, 180 * 86400);
			msg.setData(userSig);
		} catch (Exception e) {
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage("code无效");
		}

		return new ResponseEntity<>(msg, HttpStatus.OK);
	}
}
